package com.csr.csrcheck.service.impl;


import com.csr.csrcheck.mapper.Business_risksMapper;
import com.csr.csrcheck.pojo.Business_risks;
import com.csr.csrcheck.pojo.Risk_classify;
import com.csr.csrcheck.pojo.Risk_type;
import com.csr.csrcheck.service.Business_risksService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.service.ex.LawsuitException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class Business_risksServiceImpl implements Business_risksService {
    @Resource
    private Business_risksMapper business_risksMapper;

    public List<Business_risks> getBusiness_riskslist() {
        List<Business_risks> list = this.business_risksMapper.getBusiness_riskslist();
        if (list == null)
            throw new LawsuitException("");
        return list;
    }

    public PageResult getListpage(int pageNum, int pageSize, String company_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageInfo(pageNum, pageSize, company_name));
    }

    public void addBusiness(Business_risks business_risks) {
        int row = this.business_risksMapper.addBusiness(business_risks);
        if (row != 1)
            throw new CompanyException("");
    }

    public int updateBusiness(Business_risks business_risks) {
        int row = this.business_risksMapper.updateBusiness(business_risks);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int deleteBusiness(int id) {
        int row = this.business_risksMapper.deleteBusiness(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public List<Risk_classify> findbylistclassify() {
        return this.business_risksMapper.findbylistclassify();
    }

    public List<Risk_type> findbylisttype() {
        return this.business_risksMapper.findbylisttype();
    }

    public Business_risks findlistbyid(int id) {
        return this.business_risksMapper.findlistbyid(id);
    }

    private PageInfo<Business_risks> getPageInfo(int pageNum, int pageSize, String company_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Business_risks> list = this.business_risksMapper.getListpage(company_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}